package rs.frenjoynet.core.core;

import rs.frenjoynet.core.core.entity.db.Language;
import rs.frenjoynet.core.core.cache.UtilisCache;
import rs.frenjoynet.core.core.cache.ActiveUserSessionCache;
import rs.frenjoynet.core.core.db.DBBroker;
import rs.frenjoynet.core.core.db.MySqlHibDBBrokerImpl;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.HashSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;

import javax.ejb.*;

/**
 * Created by IntelliJ IDEA.
 * User: ppopovic
 * Date: 16.10.2009.
 * Time: 12.32.25
 * To change this template use File | Settings | File Templates.
 */
@EJBs
		({
				@EJB (beanInterface = CoreSecurityFacade.class, beanName = "CoreSecurityFacadeBean",
						name = "CoreSecurityFacadeBean"),
				@EJB (beanInterface = CoreApplicationsFacade.class, beanName = "CoreApplicationsFacadeBean",
						name = "CoreApplicationsFacadeBean")
		})
public abstract class BaseFacadeImpl implements BaseFacade, Serializable
{
	private static final UtilisCache utilisCache = new UtilisCache();
	//private ConnectionParametar connectionParametar;

	private Log log = LogFactory.getLog(BaseFacadeImpl.class);
	private CoreApplicationsFacade coreAppComplexFacade;
	private CoreSecurityFacade coreSecuritFacade;

	private DBBroker dbBroker;

	private String sessionGuid;
	private static final Set<Class<? extends BaseFacadeImpl>> staticInitialized = new HashSet<Class<? extends BaseFacadeImpl>>();

	public CoreApplicationsFacade getCoreAppComplexFacade()
	{
		if (coreAppComplexFacade == null)
			coreAppComplexFacade = getFacadeFactory().createAppComplexFacade();
		coreAppComplexFacade.setSessionGuid(sessionGuid);
		return coreAppComplexFacade;
	}

	public CoreSecurityFacade getCoreSecuritFacade()
	{
		if (coreSecuritFacade == null)
			coreSecuritFacade = getFacadeFactory().createSecurityFacade();
		coreSecuritFacade.setSessionGuid(sessionGuid);
		return coreSecuritFacade;
	}

	@Override
	public void setSessionGuid(String sessionGuid)
	{
		this.sessionGuid = sessionGuid;
	}

	@Override
	@Remove
	@TransactionAttribute (TransactionAttributeType.NEVER)
	public void release()
	{
		//To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public String getSessionGuid()
	{
		return null;  //To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public List<Language> getLanguages()
	{
		return null;  //To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public Language getLanguage(String languageId)
	{
		return null;  //To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public Language getLanguageByLocale(String javaLocale)
	{
		return null;  //To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public Language getDefaultLanguage()
	{
		return null;  //To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public void initialize()
	{
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public UtilisCache getUtilisCache()
	{
		return utilisCache;
	}

	protected FacadeFactory getFacadeFactory()
	{
		return new FacadeFactoryImpl();
	}

	protected ActiveUserSession getActiveUserSession(String sessionGuid)
	{
		if (sessionGuid == null)
		{
			return null;
		}
		else
		{
			ActiveUserSession session = getActiveUserSessionCache().getByGuid(sessionGuid);
			return session;
		}
	}

	protected ActiveUserSession getActiveSession()
	{
		return getActiveUserSession(sessionGuid);
	}

	protected ActiveUserSessionCache getActiveUserSessionCache()
	{
		return new ActiveUserSessionCache(getUtilisCache());
	}

	protected synchronized boolean isStaticInitialized(Class<? extends BaseFacadeImpl> clazz)
	{
		boolean result = staticInitialized.contains(clazz);
		if (!result)
		{
			staticInitialized.add(clazz);
		}
		return result;
	}

	protected DBBroker getDbBroker()
	{
		if(dbBroker==null)
		{
			dbBroker = new MySqlHibDBBrokerImpl();
		}
		return dbBroker;
	}
	

}
